import { managers } from "../../../../engines/gameEngine";
import LevelLianXian_1Item from "../base/LevelLianXian_1Item";

const { ccclass, property } = cc._decorator;

@ccclass
export default class LevelLianXian11Item extends LevelLianXian_1Item {
    moveComponentCancel(touchEvent: cc.Event.EventTouch): void {
        if (!this.level1Script.canTouch) return;
        if (!this.level1Script.isPlaySound && this.level1Script.currentSelectNode) {
            // this.level1Script.leftNode.children.forEach((n:cc.Node)=>{
            //     n.scale = 1;
            // });
            // this.level1Script.rightNode.children.forEach((n:cc.Node)=>{
            //     n.scale = 1;
            // });
            this.level1Script.currentSelectNode.scale = 1;
            this.level1Script.redrawAllPointPath();
            let tag = this.level1Script.currentSelectNode.getComponent(LevelLianXian_1Item).tagNum;
            let isIndexLeft = this.level1Script.currentSelectNode.getComponent(LevelLianXian_1Item).nodeRotation == 0;
            let currentNum = this.level1Script.afterSelectNode?.getComponent(LevelLianXian_1Item).currentNum;
            if (this.level1Script.afterSelectNode && tag.includes(currentNum) && this.level1Script.afterSelectNode && (this.level1Script.afterSelectNode.getComponent(LevelLianXian_1Item).currentNum != this.nowLianNum || this.currentNum == -1)) {
                this.level1Script.afterSelectNode.scale = 1;
                this.level1Script.redrawAllPointPath();
                this.currentColors();
                this.bingo(this.getNodePos(this.level1Script.currentSelectNode), this.getNodePos(this.level1Script.afterSelectNode), this.currentColor, this.level1Script.afterSelectNode);
                let index = this.level1Script.currentSelectNode.getComponent(LevelLianXian_1Item).currentNum;
                this.bingoAni(currentNum, index, isIndexLeft);
                this.changeAniNode(false);
                this.level1Script.afterSelectNode.getComponent(LevelLianXian_1Item).changeAniNode(false);
                this.level1Script.currentSelectNode.getComponent(LevelLianXian_1Item).itemBg.color = this.currentColor;
                this.level1Script.currentSelectNode.getComponent(LevelLianXian_1Item).itemBg.active = true;
                this.level1Script.afterSelectNode.getComponent(LevelLianXian_1Item).itemBg.color = this.currentColor;
                this.level1Script.afterSelectNode.getComponent(LevelLianXian_1Item).itemBg.active = true;
                // this.le
                let path: string = this.level1Script.levelName + "/" + this.level1Script.sounds[currentNum - 1];
                this.level1Script.isAllCheckDone();
                if (this.sigleSound != "") {

                    managers().soundMgr.stopAllSound();
                    path = this.level1Script.levelName + "/" + this.sigleSound;
                    managers().soundMgr.playSound(path, () => {
                        this.level1Script.checkWin();
                    });
                } else {

                    if (this.level1Script.sounds[currentNum - 1] && this.level1Script.sounds[currentNum - 1] != "") {
                        managers().soundMgr.playSound(path, () => {
                            this.level1Script.checkWin();
                        });
                    } else {
                        // if (this.sigleSound == "" || this.sigleSound == null) {
                            this.level1Script.checkWin();
                        // }
                    }
                }
                // if(this.sigleSound != ""){
                //     managers().soundMgr.stopAllSound();
                //     path = this.level1Script.levelName+"/sound/" + this.sigleSound;
                //     managers().soundMgr.playSound(path,()=>{
                //         // this.level1Script.checkWin();
                //     });
                // }else{
                //     if(this.level1Script.sounds[currentNum - 1] == null || this.level1Script.sounds[currentNum-1] == ""){
                //         // this.level1Script.checkWin();
                //     }
                // }

                if ((this.level1Script.sounds[currentNum - 1] == null || this.level1Script.sounds[currentNum - 1] == "") && (this.level1Script.sounds[currentNum - 1] && this.level1Script.sounds[currentNum - 1] != "")) {
                    // this.level1Script.checkWin();
                }

                /** 答对之后，停止音效 */
                if (this.level1Script.isPlaySoundNode) {
                    managers().soundMgr.stopAllSound();
                    this.level1Script.isPlaySoundNode.getComponent(LevelLianXian_1Item).isStartSoundTime = false;
                    this.level1Script.isPlaySoundNode.getComponent(LevelLianXian_1Item).currentTime = this.level1Script.isPlaySoundNode.getComponent(LevelLianXian_1Item).oldSoundTime;
                    let num2 = this.level1Script.isPlaySoundNode.getComponent(LevelLianXian_1Item).currentTime.toFixed(1);
                    this.level1Script.isPlaySoundNode.getChildByName("timeLabel").getComponent(cc.Label).string = "" + num2 + "s";
                    this.level1Script.isPlaySoundNode.getComponent(LevelLianXian_1Item).soundAni.play("soundAniIdle");
                }
                this.level1Script.isPlaySound = false;
                this.level1Script.isPlaySoundNode = null;
                this.level1Script.currentSelectNode.getComponent(LevelLianXian_1Item).isBuNengSelect = true;
                this.level1Script.currentSelectNode = null;
                this.level1Script.afterSelectNode.scale = 1;
                this.level1Script.redrawAllPointPath();
                this.level1Script.afterSelectNode = null;
                return;
            } else if (this.level1Script.afterSelectNode) {
                this.level1Script.errorSubTime();
                managers().soundMgr.playSound('mainSound/error');// 错误音效
                this.level1Script.currentSelectNode = null;
                this.level1Script.afterSelectNode.scale = 1;
                this.level1Script.redrawAllPointPath();
                this.level1Script.afterSelectNode = null;
                this.level1Script.ctx1.clear();
                this.level1Script.cishuNum = 1;
                return;
            } else {
                this.level1Script.currentSelectNode = null;
                this.level1Script.ctx1.clear();
                this.level1Script.cishuNum = 1;
                return;
            }
        }
    }
}
